<?php

class PasswordsPairValidator extends AgaviValidator
{
    
    /**
     * Check that passwords pair is valid: passwords are identical and match passwor pattern
     *
     * @return boolean
     */
    protected function validate()
    {
        $arguments = $this->getArguments();
        if (count($arguments) != 2) {
            return false;
        }
        
        $password1 =& $this->getData($arguments[0]);
        $password2 =& $this->getData($arguments[1]);
        
        // first check that two arguments are equal
        if ($password1 !== $password2) {
            $this->throwError('equals');
            return false;
        }
        
        // if both are empty then also return true
        if ($password1 === '' || $password1 === NULL) {
             return TRUE;
        }
        
        // now check password pattern
        if (preg_match('/^[a-z0-9_!@#$%^&*()~`+=\/\<>,.;:-]+$/i', $password1) == 0) {
            $this->throwError('pattern');
            return false;
        }
        
        // now check length
        if (strlen($password1) < 6) {
            $this->throwError('min');
            return false;
        }
        
        return true;
    }
    
    protected function checkAllArgumentsSet($throwError = true)
    {
        return TRUE;
    }
}
?>